{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import QuantLib as ql"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "settings = ql.Settings.instance()\n",
    "evDate = ql.Date(24, 3, 2023)\n",
    "settings.setEvaluationDate(evDate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "Cal = ql.China()\n",
    "DC360 = ql.Actual360()\n",
    "DC365 = ql.Actual365Fixed()\n",
    "settlementDays = 0\n",
    "refDate = Cal.advance(evDate, settlementDays, ql.Days, ql.Following, False)\n",
    "maturity = ql.Date.nthWeekday(3, ql.Friday, ql.September, 2023)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "d1 = ql.Date(21, 4, 2023)\n",
    "d2 = ql.Date(19, 5, 2023)\n",
    "d3 = ql.Date(16, 6, 2023)\n",
    "d4 = ql.Date(15, 9, 2023)\n",
    "d5 = ql.Date(15, 12, 2023)\n",
    "d6 = ql.Date(15, 3, 2024)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "V_TTM = ql.DateVector([d1, d2, d3, d4, d5, d6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "V_Sig = ql.DoubleVector([0.13657, 0.14378, 0.15642, 0.18078, 0.18190, 0.17445])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "blackVarTS = ql.BlackVarianceCurve(refDate, V_TTM, V_Sig, DC365)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.13997429493469776\n"
     ]
    }
   ],
   "source": [
    "sig_1 = blackVarTS.blackVol(0.1, 4000)\n",
    "print(sig_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1527208565207724\n"
     ]
    }
   ],
   "source": [
    "sig_2 = blackVarTS.blackVol(0.2, 4000)\n",
    "print(sig_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1527208565207724\n"
     ]
    }
   ],
   "source": [
    "sig_3 = blackVarTS.blackVol(0.2, 4400)\n",
    "print(sig_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "V_K = ql.DoubleVector([3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300, 4400, 4500])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "M_Sig = ql.Matrix(11, 6) # (K, TTM)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "M_Sig[0][0] = 0.21933; M_Sig[0][1] = 0.18417; M_Sig[0][2] = 0.18637; \n",
    "M_Sig[0][3] = 0.19809; M_Sig[0][4] = 0.18713; M_Sig[0][5] = 0.18013"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "M_Sig[1][0] = 0.18992; M_Sig[1][1] = 0.17050; M_Sig[1][2] = 0.17837; \n",
    "M_Sig[1][3] = 0.18924; M_Sig[1][4] = 0.18314; M_Sig[1][5] = 0.17959"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "M_Sig[2][0] = 0.17258; M_Sig[2][1] = 0.15793; M_Sig[2][2] = 0.16800; \n",
    "M_Sig[2][3] = 0.18281; M_Sig[2][4] = 0.18159; M_Sig[2][5] = 0.17886"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "M_Sig[3][0] = 0.15390; M_Sig[3][1] = 0.15244; M_Sig[3][2] = 0.16110; \n",
    "M_Sig[3][3] = 0.18532; M_Sig[3][4] = 0.17950; M_Sig[3][5] = 0.17740"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "M_Sig[4][0] = 0.14090; M_Sig[4][1] = 0.15433; M_Sig[4][2] = 0.16000; \n",
    "M_Sig[4][3] = 0.18556; M_Sig[4][4] = 0.17946; M_Sig[4][5] = 0.17476"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "M_Sig[5][0] = 0.13114; M_Sig[5][1] = 0.13971; M_Sig[5][2] = 0.15469; \n",
    "M_Sig[5][3] = 0.18707; M_Sig[5][4] = 0.18242; M_Sig[5][5] = 0.17441"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "M_Sig[6][0] = 0.15366; M_Sig[6][1] = 0.15707; M_Sig[6][2] = 0.16361; \n",
    "M_Sig[6][3] = 0.15418; M_Sig[6][4] = 0.17711; M_Sig[6][5] = 0.17486"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "M_Sig[7][0] = 0.15384; M_Sig[7][1] = 0.15503; M_Sig[7][2] = 0.16153; \n",
    "M_Sig[7][3] = 0.15579; M_Sig[7][4] = 0.17047; M_Sig[7][5] = 0.17553"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "M_Sig[8][0] = 0.15952; M_Sig[8][1] = 0.15558; M_Sig[8][2] = 0.16501; \n",
    "M_Sig[8][3] = 0.15683; M_Sig[8][4] = 0.17001; M_Sig[8][5] = 0.17535"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "M_Sig[9][0] = 0.16879; M_Sig[9][1] = 0.15714; M_Sig[9][2] = 0.17184; \n",
    "M_Sig[9][3] = 0.16190; M_Sig[9][4] = 0.17227; M_Sig[9][5] = 0.17404"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "M_Sig[10][0] = 0.18564; M_Sig[10][1] = 0.15915; M_Sig[10][2] = 0.17664; \n",
    "M_Sig[10][3] = 0.16684; M_Sig[10][4] = 0.17059; M_Sig[10][5] = 0.17808"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "blackSurface = ql.BlackVarianceSurface(refDate, Cal, V_TTM, V_K, M_Sig, DC365)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1351991095759768\n"
     ]
    }
   ],
   "source": [
    "sig_4 = blackSurface.blackVol(0.10, 4000)\n",
    "print(sig_4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.16871546416158856\n"
     ]
    }
   ],
   "source": [
    "sig_5 = blackSurface.blackVol(0.10, 4450)\n",
    "print(sig_5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.19275322215916318\n"
     ]
    }
   ],
   "source": [
    "sig_6 = blackSurface.blackVol(0.10, 3550)\n",
    "print(sig_6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.15033270186717504\n",
      "0.17155685445236235\n",
      "0.18570972996617488\n"
     ]
    }
   ],
   "source": [
    "sig_7 = blackSurface.blackVol(0.20, 4000)\n",
    "sig_8 = blackSurface.blackVol(0.20, 4500)\n",
    "sig_9 = blackSurface.blackVol(0.20, 3500)\n",
    "print(sig_7)\n",
    "print(sig_8)\n",
    "print(sig_9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
